namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    public class PaintLineRectangle : PaintLineBase
    {
        private PointF oppositePoint = new PointF();

        public PaintLineRectangle()
        {
            base.LineType = PaintLineType.Rectangle;
            base.PlName_zh = "标注矩形";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.MoveType = this.GetClickStatus();
                base.BeginMovePoint = crossHairPoint;
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
                RectangleF bound = this.GetBound();
                float x = bound.X;
                float y = bound.Y;
                float num3 = bound.X + bound.Width;
                float num4 = bound.Y;
                float num5 = bound.X;
                float num6 = bound.Y + bound.Height;
                float num7 = bound.X + bound.Width;
                float num8 = bound.Y + bound.Height;
                switch (base.MoveType)
                {
                    case 1:
                        this.oppositePoint = new PointF(num7, num8);
                        break;

                    case 2:
                        this.oppositePoint = new PointF(num5, num6);
                        break;

                    case 3:
                        this.oppositePoint = new PointF(num3, num4);
                        break;

                    case 4:
                        this.oppositePoint = new PointF(x, y);
                        break;
                }
                if (base.MoveType == 0)
                {
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                else
                {
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                RectangleF bound = this.GetBound(pList);
                bound = new RectangleF(bound.X - base.CgInstance.LeftYScaleWidth, (bound.Y - div.Rect.Y) - div.TitleHeight, bound.Width, bound.Height);
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                g.DrawRectangle(pen, bound.X, bound.Y, bound.Width, bound.Height);
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(bound.X - (base.LineWidth * 2.5f), bound.Y - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF((bound.X + bound.Width) - (base.LineWidth * 2.5f), bound.Y - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(bound.X - (base.LineWidth * 2.5f), (bound.Y + bound.Height) - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF((bound.X + bound.Width) - (base.LineWidth * 2.5f), (bound.Y + bound.Height) - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                brush.Dispose();
                pen.Dispose();
            }
        }

        public RectangleF GetBound()
        {
            return this.GetBound(base.PointList);
        }

        public RectangleF GetBound(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count == 0)
            {
                return new RectangleF();
            }
            PaintLineBase.PointValue value2 = pList[0];
            double chartValue = value2.Value;
            PaintLineBase.PointValue value3 = pList[1];
            double num2 = value3.Value;
            PaintLineBase.PointValue value4 = pList[0];
            int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
            PaintLineBase.PointValue value5 = pList[1];
            int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
            float x = base.CgInstance.GetX(pos);
            float y = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
            float num7 = base.CgInstance.GetX(num4);
            float num8 = base.CgInstance.GetY(base.OwnedPanelID, num2, null);
            float width = num7 - x;
            if (width < 4f)
            {
                width = 4f;
            }
            float height = num8 - y;
            if (height < 4f)
            {
                height = 4f;
            }
            return new RectangleF(x, y, width, height);
        }

        public int GetClickStatus()
        {
            Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
            RectangleF bound = this.GetBound();
            float x = bound.X;
            float y = bound.Y;
            float num3 = bound.X + bound.Width;
            float num4 = bound.Y;
            float num5 = bound.X;
            float num6 = bound.Y + bound.Height;
            float num7 = bound.X + bound.Width;
            float num8 = bound.Y + bound.Height;
            if (((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (y - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (y + (base.LineWidth * 2.5f)))))
            {
                return 1;
            }
            if (((crossHairPoint.X >= (num3 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num3 + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num4 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num4 + (base.LineWidth * 2.5f)))))
            {
                return 2;
            }
            if (((crossHairPoint.X >= (num5 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num5 + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num6 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num6 + (base.LineWidth * 2.5f)))))
            {
                return 3;
            }
            if (((crossHairPoint.X >= (num7 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num7 + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num8 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num8 + (base.LineWidth * 2.5f)))))
            {
                return 4;
            }
            RectangleF ef2 = new RectangleF(bound.X - (base.LineWidth * 2.5f), bound.Y - (base.LineWidth * 2.5f), bound.Width + (base.LineWidth * 5f), bound.Height + (base.LineWidth * 5f));
            if (ef2.Contains((PointF) crossHairPoint))
            {
                if ((bound.Width <= 4f) || (bound.Height <= 4f))
                {
                    return 0;
                }
                RectangleF ef3 = new RectangleF(bound.X + (base.LineWidth * 2.5f), bound.Y + (base.LineWidth * 2.5f), bound.Width - (base.LineWidth * 5f), bound.Height - (base.LineWidth * 5f));
                if (!ef3.Contains((PointF) crossHairPoint))
                {
                    return 0;
                }
            }
            return -1;
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
                double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
                base.PointList.Clear();
                base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                base.PointList.Add(new PaintLineBase.PointValue(1, key, valueByPoint));
                return true;
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count == 0)
            {
                return false;
            }
            this.GetBound();
            base.CgInstance.GetCrossHairPoint();
            return (this.GetClickStatus() != -1);
        }

        public override double[] LineParam()
        {
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num3 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            base.CgInstance.GetValueByPoint(crossHairPoint);
                            double num4 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num5 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num6 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num7 = (num4 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num6 - num5);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            int num8 = (base.CgInstance.GetRecordByTime(value2.Key) + (num3 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[1];
                            int num9 = (base.CgInstance.GetRecordByTime(value3.Key) + (num3 - indexByPoint)) - 1;
                            if (num8 >= 0)
                            {
                                if (num8 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num8 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num8 = 0;
                            }
                            if (num9 < 0)
                            {
                                num9 = 0;
                            }
                            else if (num9 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num9 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num8][base.CgInstance.CalenderScaleField].ToString(), value4.Value + num7);
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num9][base.CgInstance.CalenderScaleField].ToString(), value5.Value + num7);
                            return;
                        }
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        {
                            double valueByPoint = base.CgInstance.GetValueByPoint(new Point((int) this.oppositePoint.X, (int) this.oppositePoint.Y));
                            double num11 = base.CgInstance.GetValueByPoint(crossHairPoint);
                            int num12 = base.CgInstance.GetIndexByPoint(new Point((int) this.oppositePoint.X, (int) this.oppositePoint.Y));
                            int num13 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            double num14 = 0.0;
                            double num15 = 0.0;
                            if (valueByPoint < num11)
                            {
                                num14 = num11;
                                num15 = valueByPoint;
                            }
                            else
                            {
                                num14 = valueByPoint;
                                num15 = num11;
                            }
                            string key = string.Empty;
                            string str2 = string.Empty;
                            if (num12 < 0)
                            {
                                num12 = 0;
                            }
                            else if (num12 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num12 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            if (num13 < 0)
                            {
                                num13 = 0;
                            }
                            else if (num13 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num13 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            if (num12 >= num13)
                            {
                                key = base.CgInstance.DataSource.Rows[num13][base.CgInstance.CalenderScaleField].ToString();
                                str2 = base.CgInstance.DataSource.Rows[num12][base.CgInstance.CalenderScaleField].ToString();
                            }
                            else
                            {
                                key = base.CgInstance.DataSource.Rows[num12][base.CgInstance.CalenderScaleField].ToString();
                                str2 = base.CgInstance.DataSource.Rows[num13][base.CgInstance.CalenderScaleField].ToString();
                            }
                            base.PointList[0] = new PaintLineBase.PointValue(0, key, num14);
                            base.PointList[1] = new PaintLineBase.PointValue(1, str2, num15);
                            return;
                        }
                    }
                }
            }
        }
    }
}

